﻿#include "dayincalendarfragment.h"
#include "ui_dayincalendarfragment.h"
#include "editeventfragment.h"
#include "eventtimeout.h"
#include <QMessageBox>
#include "loader.h"
#include "eventfragment.h"
#include <QListWidgetItem>
#include <QDebug>
DayInCalendarFragment::DayInCalendarFragment(QMap<QString, Event> events,int year,int month,int dayNumber, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DayInCalendarFragment) {
    ui->setupUi(this);
    this->events = events;
    this->dayNumber = dayNumber;
    ui->label_day->setText(QString::number(dayNumber));
    for(auto x:events.keys()) {
        ui->listWidget_showEvents->addItem(x);
    }
    for(auto &x:events.values())x.setNeedRing(true);
    this->year = year;
    this->month = month;
    ui->label_month->setText(monthName[(month+dayNumber)%12]);
    this->timer.setInterval(500);
    connect(&timer,&QTimer::timeout,this,&DayInCalendarFragment::bindTimer);
    timer.start();

}

DayInCalendarFragment::~DayInCalendarFragment() {
    delete ui;
}

void DayInCalendarFragment::on_pushButton_add_clicked() {
    Event event = EditEventFragment::getEvent();
    if(event.getName().isEmpty())return;
    qDebug()<<event.getImportance();
    if(this->events.contains(event.getName())) {
        QMessageBox::warning(this,tr("Error"),tr("A event with the same name has already existed"));
        return;
    }
    this->events[event.getName()] = event;
    ui->listWidget_showEvents->addItem(event.getName());
    QVector<Event> eves;
    for(auto &x:this->events.values())eves.push_back(x);
    Loader::saveEvents(eves,QString("./savings/calendar_event_%1_%2_%3.json").arg(year).arg(month).arg(dayNumber));
    QMessageBox::information(this,tr("OK"),tr("Success!"));
}


void DayInCalendarFragment::on_pushButton_del_clicked() {
    QList<QListWidgetItem *> items = ui->listWidget_showEvents->selectedItems();
    if(items.empty())QMessageBox::warning(this,tr("Error"),tr("No item selected"));
    else {
        for(auto x:items) {
            this->events[x->text()].setNeedRing(false);
            this->events.remove(x->text());
        }
        ui->listWidget_showEvents->clear();
        for(auto &x:events.keys())ui->listWidget_showEvents->addItem(x);
        for(auto &x:events.values())x.setNeedRing(true);
        QVector<Event> eves;
        for(auto &x:this->events.values())eves.push_back(x);
        Loader::saveEvents(eves,QString("./savings/calendar_event_%1_%2_%3.json").arg(year).arg(month).arg(dayNumber));
        QMessageBox::information(this,tr("OK"),tr("Success"));
    }

}

void DayInCalendarFragment::on_pushButton_det_clicked() {
    QList<QListWidgetItem *> items = ui->listWidget_showEvents->selectedItems();
    if(items.empty())QMessageBox::warning(this,tr("Error"),tr("No item selected"));
    else {
        for(auto x:items) {
            EventFragment *frag = new EventFragment(this->events[x->text()]);
            connect(frag,&EventFragment::onEventChange,this,&DayInCalendarFragment::onEventChange);
            frag->show();
        }

    }
}

void DayInCalendarFragment::onEventChange(Event eve) {
    this->events[eve.getName()] = eve;
    ui->listWidget_showEvents->clear();
    for(auto x:events.keys())ui->listWidget_showEvents->addItem(x);
    for(auto &x:events.values())x.setNeedRing(true);
    QVector<Event> eves;
    for(auto &x:this->events.values())eves.push_back(x);
    Loader::saveEvents(eves,QString("./savings/calendar_event_%1_%2_%3.json").arg(year).arg(month).arg(dayNumber));
}

void DayInCalendarFragment::bindTimer() {
    qint16 curr = QDateTime::currentSecsSinceEpoch();
    for(auto &x:this->events.values()) {
        if(curr>=x.getTime().toSecsSinceEpoch()&&!ringed.contains(x.getName()+x.getTime().toSecsSinceEpoch())) {
            ringed[x.getName()+x.getTime().toSecsSinceEpoch()] = true;
            EventTimeout *dialog = new EventTimeout(x.getName(),x.getImportance());
            qDebug()<<"name:"<<x.getName()<<"importance:"<<x.getImportance();
            dialog->show();
        }
    }
}
